package com.nandi.deviceinstall.base

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.multidex.MultiDex
import com.nandi.deviceinstall.R
import com.nandi.deviceinstall.util.Density
import com.nandi.deviceinstall.util.DynamicTimeFormat
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.uuzuche.lib_zxing.activity.ZXingLibrary

/**
 * Date:2020-01-14
 * Time:16:34
 * author:qingsong
 */
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        MultiDex.install(this)
        ZXingLibrary.initDisplayOpinion(this);
        //initRefreshLayout()
        registerActivityLifecycleCallbacks(object :ActivityLifecycleCallbacks{
            override fun onActivityCreated(p0: Activity?, p1: Bundle?) {
                Density.setDensity(this@MyApp, p0!!)
            }

            override fun onActivityPaused(p0: Activity?) {
            }

            override fun onActivityResumed(p0: Activity?) {
            }

            override fun onActivityStarted(p0: Activity?) {
            }

            override fun onActivityDestroyed(p0: Activity?) {
            }

            override fun onActivitySaveInstanceState(p0: Activity?, p1: Bundle?) {
            }

            override fun onActivityStopped(p0: Activity?) {
            }
        })
    }

    private fun initRefreshLayout() {
        //设置全局默认配置（优先级最低，会被其他设置覆盖）
        SmartRefreshLayout.setDefaultRefreshInitializer { context, layout ->
            //全局设置（优先级最低）
            layout.setEnableAutoLoadMore(true)
            layout.setEnableOverScrollDrag(false)
            layout.setEnableOverScrollBounce(true)
            layout.setEnableLoadMoreWhenContentNotFull(true)
            layout.setEnableScrollContentWhenRefreshed(true)
            layout.setEnableFooterFollowWhenNoMoreData(true)
            layout.setPrimaryColorsId(R.color.white, R.color.gray)
        }

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(object : DefaultRefreshHeaderCreator {
            override fun createRefreshHeader(
                    context: Context,
                    layout: RefreshLayout
            ): RefreshHeader {
                return ClassicsHeader(context).setTimeFormat(DynamicTimeFormat(""))//指定为经典Header，默认是 贝塞尔雷达Header
            }
        })
        //设置全局的Footer构建器
//        SmartRefreshLayout.setDefaultRefreshFooterCreator(object : DefaultRefreshFooterCreator {
//            override fun createRefreshFooter(
//                context: Context,
//                layout: RefreshLayout
//            ): RefreshFooter {
//                //指定为经典Footer，默认是 BallPulseFooter
//                layout.setPrimaryColorsId(R.color.white, R.color.gray)
//                return ClassicsFooter(context).setDrawableSize(20f)
////                return BallPulseFooter(context)
//            }
//        })
    }
}
